/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import entity.Groups;
import entity.Orders;
import entity.Role;
import entity.Warehouse;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Svetlana Shikhutskaya
 */
public class JSON {
    public String ordersToJSON(Collection<Orders> collection) {
        StringBuilder line = new StringBuilder();
        int i = 0;
        line.append("[");
        for (Orders order : collection) {
            if (i == 0) {
                line.append("{\"id\":\"").append(order.getTrackingNumber()).append("\",");
                i++;
            } else {
                line.append(",{\"id\":\"").append(order.getTrackingNumber()).append("\",");
            }
            String orderDate = new SimpleDateFormat("dd-MM-yyyy").format(order.getOrderDate());
            line.append("\"date\":\"").append(orderDate).append("\",");
            if (order.getOrderStatus().equals("delivered")) {
                String deliveryDate = new SimpleDateFormat("dd-MM-yyyy").format(order.getDeliveryDate());
                line.append("\"due-date\":\"").append(deliveryDate).append("\",");
            } else {
                line.append("\"due-date\":\"None\",");
            }
            line.append("\"sum\":\"").append(order.getProductPrice()).append("\",");
            line.append("\"address\":\"").append(order.getAddress()).append("\",");
            line.append("\"status\":\"").append(order.getOrderStatus()).append("\"}");
        }
        line.append("]");
        return line.toString();
    }

    public String warehousesToJSON(Collection<Warehouse> collection) {
        StringBuilder line = new StringBuilder();
        int i = 0;
        line.append("[");
        for (Warehouse w : collection) {
            if (i == 0) {
                line.append("{\"name\":\"").append(w.getName()).append("\"");
                i++;
            } else {
                line.append(",{\"name\":\"").append(w.getName()).append("\"");
            }
            line.append(",\"id\":\"").append(w.getIdWarehouse()).append("\"");
            line.append(",\"address\":\"").append(w.getAddress()).append("\"");
            line.append(",\"rate\":\"").append(w.getShipmentRate()).append("\"");
            line.append(",\"koeff\":\"").append(w.getDurationKoefficient()).append("\"}");
        }
        line.append("]");
        return line.toString();
    }

    public String groupToJSON(Collection<Groups> collection) {
        StringBuilder line = new StringBuilder();
        line.append("[");
        int i = 0;
        for (Groups gr : collection) {
            if (i != 0) {
                line.append(",");
            }
            i++;
            line.append("{\"name\":\"").append(gr.getGroupName()).append("\",");
            line.append("\"id\":\"").append(gr.getIdGroup()).append("\",");
            String roles = toLine((List<Role>) gr.getRoleCollection());
            line.append("\"roles\":[").append(roles).append("]}");
        }
        line.append("]");
        return line.toString();
    }

    private String toLine(List<Role> list) {
        StringBuilder line = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            if (i != 0) {
                line.append(",\"").append(list.get(i).getRoleName()).append("\"");
            } else {
                line.append("\"").append(list.get(i).getRoleName()).append("\"");
            }
        }
        return line.toString();
    }

    public String ordersForReportToJSON(List<Orders> list) {
        StringBuilder line = new StringBuilder();
        int i = 0;
        line.append("[");
        for (Orders order : list) {
            String orderDate = new SimpleDateFormat("dd-MM-yyyy").format(order.getOrderDate());
            if (i == 0) {
                line.append("{\"trackingNumber\":\"").append(order.getTrackingNumber()).append("\",");
                i++;
            } else {
                line.append(",{\"trackingNumber\":\"").append(order.getTrackingNumber()).append("\",");
            }

            line.append("\"date\":\"").append(orderDate).append("\",");

            if (order.getOrderStatus().equals("delivered")) {
                String deliveryDate = new SimpleDateFormat("dd-MM-yyyy").format(order.getDeliveryDate());
                line.append("\"deliveryDate\":\"").append(deliveryDate).append("\",");
            } else {
                line.append("\"deliveryDate\":\"None\",");
            }
            line.append("\"warehouse\":\"").append(order.getIdWarehouse().getName()).append("\",");
            line.append("\"shippingPrice\":\"").append(order.getShippingPrice()).append("\",");
            line.append("\"productPrice\":\"").append(order.getProductPrice()).append("\",");
            line.append("\"address\":\"").append(order.getAddress()).append("\",");
            String user = "";
            if (order.getIdUser() == null) {
                user = "None";
            } else {
                user = order.getIdUser().getFirstName() + " " + order.getIdUser().getLastName();
            }
            line.append("\"user\":\"").append(user).append("\",");
            line.append("\"status\":\"").append(order.getOrderStatus()).append("\"}");
        }
        line.append("]");
        return line.toString();
    }
}
